#include <stdio.h>
#include <stdlib.h>

#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <time.h>

#include <unistd.h>

#define IPSTRSIZE 40
#define BUFSIZE 1024

int main(int argc, char *argv[])
{
    if (argc < 2)
    {
        fprintf(stderr, "Using ...\n");
        exit(1);
    }

    int sd = socket(AF_INET, SOCK_STREAM, 0);
    if (sd < 0)
    {
        perror("socket()");
        exit(1);
    }

    struct sockaddr_in raddr;
    raddr.sin_family = AF_INET;
    raddr.sin_port = htons(80);
    inet_pton(AF_INET, argv[1], &raddr.sin_addr);

    if (connect(sd, (struct sockaddr *)&raddr, sizeof(raddr)) < 0)
    {
        perror("Connection()");
        exit(1);
    }

    //将文件描述符转换成标准流
    FILE *fp = fdopen(sd, "r+");
    if (fp == NULL)
    {
        perror("fdopen()");
        exit(1);
    }

    fprintf(fp, "GET /test.jpg\r\n\r\n");
    fflush(fp);

    char *rbuf[BUFSIZE];
    while (1)
    {
        int len = fread(rbuf, 1, BUFSIZE, fp);
        fwrite(rbuf, 1,len, stdout);
    }
    
   
    fclose(fp);
    exit(0);
}